<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="订单状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="游客姓名" prop="orderTname">
        <el-input
          v-model="queryParams.orderTname"
          placeholder="请输入游客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游客手机号" prop="orderTphone">
        <el-input
          v-model="queryParams.orderTphone"
          placeholder="请输入游客手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单日期" prop="orderTime">
        <el-date-picker
          v-model="queryParams.orderTime"
          type="date"
          clearable
          value-format="yyyy-MM-dd"
          placeholder="选择日期"/>
      </el-form-item>
      <el-form-item label="订单分类" prop="orderType">
        <el-select
          v-model="queryParams.orderType"
          placeholder="订单分类"
          clearable
        >
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="游客姓名" align="center" prop="orderTname" />
      <el-table-column label="游客手机号" align="center" prop="orderTphone" />
      <el-table-column label="下单日期" align="center" prop="orderTime" />
      <el-table-column label="状态" align="center" prop="orderStatus" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="orderPayType" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.pay_way" :value="scope.row.orderPayType"/>
          </template>
      </el-table-column>
      <el-table-column label="收货人姓名" align="center" prop="deliveryName" />
      <el-table-column label="收货人电话" align="center" prop="deliveryPhone" />
      <el-table-column label="地址类型" align="center" prop="addressType" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.address_type" :value="scope.row.addressType"/>
          </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="addressDetail" >
        <template slot-scope="scope">
          <span>{{handleAddressDetail(scope.row.addressDetail)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if='scope.row.orderStatus==1'
            @click="handleCancel(scope.row)"
            v-hasPermi="['order:order:edit']"
          >订单取消</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-money"
            v-if='scope.row.orderStatus!=1 && scope.row.orderStatus!=7 && scope.row.orderStatus!=6 && scope.row.orderStatus!=3 && scope.row.orderPayType==1'
            @click="handleMoney(scope.row)"
            v-hasPermi="['pay:refundOrder:refundOrder']"
          >订单退款</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-office-building"
            @click="detailDialog(scope.row)"
            v-hasPermi="['order:order:remove']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="游客姓名" prop="orderTname">
          <el-input v-model="form.orderTname" placeholder="请输入下单人姓名" />
        </el-form-item>
        <el-form-item label="游客手机号" prop="orderTphone">
          <el-input v-model="form.orderTphone" placeholder="请输入下单人联系方式" />
        </el-form-item>
        <el-form-item label="游客身份证号" prop="orderTcode">
          <el-input v-model="form.orderTcode" placeholder="请输入下单人身份证号" />
        </el-form-item>
        <el-form-item label="总金额" prop="orderTotalPrice">
          <el-input v-model="form.orderTotalPrice" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="邮费" prop="postagePrice">
          <el-input v-model="form.postagePrice" placeholder="请输入邮费" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <details-child ref='detailsChildRefGet'/>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder ,cancelOrder,refundOrder} from "@/api/order/order";
import detailsChild from '@/views/order/details/index'
export default {
  name: "Order",
  components:{
    detailsChild
  },
  dicts:['order_status','order_type','pay_way','address_type'],
  data() {
    return {
      // 退款按钮状态
      tkLodaing:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        orderTname: null,
        orderTphone: null,
        orderTcode: null,
        orderTime: null,
        orderStatus: null,
        orderPayType: null,
        orderTotalPrice: null,
        userId: null,
        orderAddressId: null,
        postagePrice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNum: null,
        orderTname: null,
        orderTphone: null,
        orderTcode: null,
        orderTime: null,
        orderStatus: null,
        orderPayType: null,
        orderTotalPrice: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        userId: null,
        orderAddressId: null,
        postagePrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    // 详情弹框
    detailDialog(row){
      console.log(this.$refs.detailsChildRefGet)
      this.$refs.detailsChildRefGet.isOpen(row.orderId)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    // 查看详情，弹出详情页签页面
    handleDetail(row){
      // this.$router.push({path:'/order/detail',query:{orderId:row.orderId}})
      this.$router.push("/order/details/detail/" + row.orderId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消订单
    handleCancel(row){
      const orderIds = row.orderId || this.ids;
      console.log(orderIds,row)
      this.$modal.confirm('是否确认取消订单编号为"' + orderIds + '"的数据项？').then(function() {
        return cancelOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消订单成功");
      }).catch(() => {});
    },
    // 订单退款
    handleMoney(row){
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认退款订单编号为"' + orderIds + '"的数据项？').then(function() {
        // this.tkLodaing=true
        console.log(orderIds)
        return refundOrder(orderIds);
      }).then(() => {
        // this.tkLodaing=false
        this.getList();
        this.$modal.msgSuccess("订单退款成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    handleAddressDetail(val) {
      if (val) {
        let res = val.split("@@");
        if (res.length > 1) {
          return res[0] + "—" + res[1]
        }
        return val;
      }
    }
  }
};
</script>
